// bootstrap
// ng启动阶段是 config-->run-->compile/link
// import("#ngx-app/bootstrap.ui-router");
// TODO: add to pack.config.cjs
// import "virtual:useRemoteEntry";

// require.context("#lit-comp", false, /\.test\.ts/);
import("#lit-comp/index");
// class RootController extends RootRoutingController {Ap
//   static $inject = RootRoutingController.$inject;
// }

/**
 * @desc define_LoadInterceptors
 */
window.name = "NG_DEFER_BOOTSTRAP!";
/**
 * @returns {Promise<void>}
 */
async function mountApp() {
  const initApp = await import("./App.effect.js").then((m) => m.default);
  // console.trace("mountApp");
  const App = initApp();
  // global register
  if (!window.__App__) {
    window.__App__ = App;
    window.__AppInfo__ = App.info;
  }
  // 加载router
  const { initRouter, RootRoutingController } = await import(
    /* webpackPreload: true */
    /* webpackFetchPriority: "high" */
    "./routers/index.mjs"
  ).then((m) => m);
  // config 可以 像 service 一样注入 provider
  App.config(initRouter);
  App.config([
    "$anchorScrollProvider",
    "$animateProvider",
    "$compileProvider",
    "$controllerProvider",
    "$filterProvider",
    "$httpProvider",
    "$interpolateProvider",
    "$locationProvider",
    "$logProvider",
    "$parseProvider",
    "$qProvider",
    "$rootScopeProvider",
    "$sceDelegateProvider",
    "$sceProvider",
    "$templateRequestProvider",
    "$injector",
    "$provide",
    /**
     * @param {ng.IAnchorScrollProvider} $anchorScrollProvider
     * @param {ng.animate.IAnimateProvider} $animateProvider
     * @param {ng.ICompileProvider} $compileProvider
     * @param {ng.IControllerProvider} $controllerProvider
     * @param {ng.IFilterProvider} $filterProvider
     * @param {ng.IHttpProvider} $httpProvider
     * @param {ng.IInterpolateProvider} $interpolateProvider
     * @param {ng.ILocationProvider} $locationProvider
     * @param {ng.ILogProvider} $logProvider
     * @param {ng.IParseProvider} $parseProvider
     * @param {ng.IQProvider} $qProvider
     * @param {ng.IRootScopeProvider} $rootScopeProvider
     * @param {ng.ISCEDelegateProvider} $sceDelegateProvider
     * @param {ng.ISCEProvider} $sceProvider
     * @param {ng.ITemplateRequestProvider} $templateRequestProvider
     * @param {ng.auto.IInjectorService} $injector
     * @param {ng.auto.IProvideService} $provide
     */
    (
      $anchorScrollProvider,
      $animateProvider,
      $compileProvider,
      $controllerProvider,
      $filterProvider,
      $httpProvider,
      $interpolateProvider,
      $locationProvider,
      $logProvider,
      $parseProvider,
      $qProvider,
      $rootScopeProvider,
      $sceDelegateProvider,
      $sceProvider,
      $templateRequestProvider,
      $injector,
      $provide,
    ) => {
      // console.log("$anchorScrollProvider", $anchorScrollProvider);
      // console.log("$animateProvider", $animateProvider);
      // console.log("$compileProvider", $compileProvider);
      // console.log("$controllerProvider", $controllerProvider);
      // console.log("$filterProvider", $filterProvider);
      // console.log("$httpProvider", $httpProvider);
      // console.log("$interpolateProvider", $interpolateProvider);
      // console.log("$locationProvider", $locationProvider);
      // console.log("$logProvider", $logProvider);
      // console.log("$parseProvider", $parseProvider);
      // console.log("$qProvider", $qProvider);
      // console.log("$rootScopeProvider", $rootScopeProvider);
      // console.log("$sceDelegateProvider", $sceDelegateProvider);
      // console.log("$sceProvider", $sceProvider);
      // console.log("$templateRequestProvider", $templateRequestProvider);
      // console.log("$injector", $injector);
      // console.log("$provide", $provide);
    },
  ]);

  App.controller("RootRoutingController", RootRoutingController);
  const AppRootComponent = await import("./AppRoot.component.html").then(
    (m) => m.default,
  );
  // run 可以像 controller  一样注入 service, 也可以注入 provider, 但是运行在config后
  App.run([
    "$templateCache",
    /**
     * @param {angular.ITemplateCacheService} $templateCache
     * @returns {void}
     */
    ($templateCache) => {
      $templateCache.put("AppRoot.component.html", AppRootComponent);
    },
  ]);
  App.factory("superCache", [
    "$cacheFactory",
    /**
     * @param {angular.ICacheFactoryService} $cacheFactory
     * @returns {angular.ICacheObject}
     */
    ($cacheFactory) => $cacheFactory("super-cache"),
  ]);
  App.component(
    /* <app-root> */ "appRoot",
    await import("./AppRoot.component.js").then((m) => m.default),
  );
  // #__DEV_ONLY__
  // import.meta.env.NODE_ENV === "development" && App.run(useRouterVisualizer);
  const NgSuspense = await import("./directives/Suspense.directive.mjs").then(
    (m) => m.default,
  );
  const ErrorBoundary = await import(
    "./directives/ErrorBoundary.component.mjs"
  ).then((m) => m.default);
  App.directive(...ErrorBoundary);
  App.directive(...NgSuspense);
}

export default mountApp;

// if (import.meta.webpackHot) {
//   import.meta.webpackHot.accept()
//   //   import.meta.webpackHot.accept(
// //     [
// //     "./routers/index.mjs",
// //     "./view/root-routing.mjs",
// //     "./App.mjs",
// //   ], ()=> {
// //     window.__App__?.component(camelCase("app-root"), AppRootView);
// //   }
// // );
// }
